package edu.gyc.test1;

import javax.swing.*;
import java.awt.*;

class MyPanel1 extends JPanel{
    private int x=200;
    private int y=200;
    private Image image;
    private Graphics graphics;

    public void display() {
        x++;
        y++;
        if (graphics == null) {
            image = this.createImage(this.getWidth(), this.getHeight());
            if (image != null) {
                graphics=image.getGraphics();
            }
        }

        if (graphics != null) {
            super.paint(graphics);
            graphics.setColor(Color.red);
            graphics.fill3DRect(x,y,100,100,true);
        }
        this.repaint();
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(image, 0, 0, this);
    }
}

public class PanelTest1 {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        MyPanel1 myPanel=new MyPanel1();
        //jFrame.setSize(800,600);
        jFrame.setBounds(200,300,800,600);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setContentPane(myPanel);
        jFrame.setVisible(true);

        while (true) {
            myPanel.display();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
